<template>
  <div class="left-column">
    <div class="column-card">
      <div class="card-header">
        <div style="display: flex;align-items: center;gap: 3px">
          <i class="fas fa-calendar-alt" aria-hidden="true"/>
          <h3>今日课程</h3>
        </div>
        <div class="view-switcher">
          <el-button
            icon="fa-solid fa-signature"
            size="mini"
            :class="{ 'active': true }"
            @click="$router.push('/course')"
          >
            排课预约
          </el-button>
          <el-button
            icon="fa-solid fa-arrows-rotate"
            size="mini"
            :class="{ 'reload-data': true }"
            @click="refreshSchedule"
          >
            刷新
          </el-button>
          <span class="badge">{{ courses.length }}</span>
        </div>
      </div>

      <div class="calendar-content">
        <ul class="schedule-list">
          <li v-for="(item,index) in courses" :key="index" class="schedule-item">
            <span v-if="item.status==='5'" class="postponement">{{ item.courseDate }}</span>
            <span class="time">{{ item.startHM }} ~ {{ item.endHM }}</span>
            <span class="content">{{ item.student }}【{{ item.type }}】【{{ item.courseDuration }}分钟课】</span>
            <span class="platform">{{ item.location }}</span>
            <span v-if="item.status==='0'" class="platform-wait">待上课</span>
            <span v-if="item.status==='1'" class="platform-wait platform-close">已取消</span>
            <span v-if="item.status==='2'" class="platform-wait platform-ing">上课中</span>
            <span v-if="item.status==='3'" class="platform-wait platform-end">已完成</span>
            <span v-if="item.status==='4'" class="platform-wait platform-close2">中途取消</span>
            <span v-if="item.status==='5'" class="platform-wait platform-postponement">已延期</span>
            <el-button v-if="item.status==='3'" class="action-btn" style="margin-left: 10px"
                       @click="printAfterCourse(item)"
            >
              <i class="fa-solid fa-print" aria-hidden="true"/>
              <span>打印</span>
            </el-button>
            <el-button v-if="item.status==='2'" class="over-btn" @click="endCourse(item)">
              <i class="fa-solid fa-power-off"/>
              <span>提前结课</span>
            </el-button>
            <el-button class="action-btn" @click="handleCourse(item)">
              <i v-if="item.status!=='3'" class="fas fa-video" aria-hidden="true"/>
              <i v-if="item.status==='3'" class="fa-solid fa-eye" aria-hidden="true"/>
              <span v-if="item.status==='0'">开始上课</span>
              <span v-if="item.status==='2'">进入课堂</span>
              <span v-if="item.status==='3'">查看课后</span>
              <span v-if="item.status==='5'">开始上课</span>
            </el-button>
          </li>
        </ul>
      </div>

      <!-- 空状态 -->
      <div v-if="courses.length === 0" class="empty-state">
        <i class="fas fa-calendar-day" aria-hidden="true"/>
        <p>今日暂无排课</p>
      </div>
    </div>
  </div>
</template>

<script>
import { earlyCompletionOfClasses, monthlyCourses, startClass } from '@/api/wordSystemCourse'

export default {
  name: 'TodayCourseColumn',
  data() {
    return {
      task1Done: false,
      task2Done: false,
      task3Done: false,
      courses: []
    }
  },
  mounted() {
    this.refreshSchedule()
  },
  methods: {
    printAfterCourse(item) {
      window.open(`${window.location.origin}${window.location.pathname}#/print?cid=${item.id}`)
    },
    async handleCourse(item) {
      if (item.status === '0' || item.status === '5' || item.status === '2') {
        if (item.status === '0' || item.status === '5') {
          await startClass({ cid: item.id })
          setTimeout(() => {
            this.refreshSchedule()
          }, 5000)
        }
        window.open(
          `${window.location.origin}${window.location.pathname}#/traditionalTeaching?q1=${item.id}`,
          '_blank',
          'width=600,height=800,top=100,left=200'
        )
      } else if (item.status === '3') {
        window.open(
          `${window.location.origin}${window.location.pathname}#/afterClass?cid=${item.id}`,
          '_blank',
          'width=600,height=800,top=100,left=200'
        )
      }
    },
    async endCourse(course) {
      this.$msgbox({
        title: '友情提示',
        message: '是否提前结课？',
        showCancelButton: true,
        confirmButtonText: '提前结课',
        cancelButtonText: '我点错了',
        type: 'warning'
      }).then(async() => {
        try {
          await earlyCompletionOfClasses()
        } finally {
          this.$router.push({ path: `/afterClass?cid=${course.id}` })
        }
      }).catch(() => {
      })
    },
    // 刷新今日课程数据
    async refreshSchedule() {
      const res = await monthlyCourses({ todayData: true })
      this.courses = []
      res.forEach(item => {
        this.courses.push({
          id: item.id,
          title: item.student + ' ' + item.courseType,
          student: item.student,
          type: item.courseType,
          location: item.location,
          notes: item.notes,
          stuId: item.stuId,
          courseDurationTypeId: item.courseDurationType,
          courseDuration: item.courseDuration,
          status: item.status,
          startHM: this.formatHM(new Date(item.startTimeTimestamp * 1000)),
          endHM: this.formatHM(new Date(item.endTimeTimestamp * 1000)),
          courseDate: this.formatYMD(new Date(item.startTimeTimestamp * 1000))
        })
      })
    },
    // 工具函数：格式化时分
    formatHM(isoString) {
      const d = new Date(isoString)
      return d.getHours().toString().padStart(2, '0') + ':' + d.getMinutes().toString().padStart(2, '0')
    },
    formatYMD(isoString) {
      const d = new Date(isoString)
      return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
    }
  }
}
</script>

<style scoped>
.left-column {
  min-width: 600px;
  box-sizing: border-box;
}

.column-card {
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(8, 8, 8, 0.15);
  padding: 15px 20px;
  background-color: #fff;
}

/* 卡片头部样式 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #f0f0f0;
}

.card-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 500;
  display: flex;
  align-items: center;
  color: #333;
}

.card-header h3 i {
  margin-right: 8px;
  color: #0084ff;
}

/* 视图切换器 */
.view-switcher {
  display: flex;
  gap: 8px;
}

.view-switcher .el-button {
  padding: 4px 10px;
  border-radius: 12px;
  background-color: #f8f9fa;
  border: none;
  color: #666;
}

.view-switcher .el-button.active {
  background-color: #0084ff;
  color: #fff;
}

.view-switcher .el-button.reload-data {
  background-color: #ae5ef2;
  color: #fff;
}

/* 日程列表样式 */
.schedule-list {
  list-style: none;
  padding: 0 10px 0 0;
  margin: 0;
  max-height: 400px;
  overflow-y: auto;
}

/* 滚动条样式优化 */
.schedule-list::-webkit-scrollbar {
  width: 6px;
}

.schedule-list::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 3px;
}

.schedule-list::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 3px;
}

.schedule-list::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

.schedule-item {
  display: flex;
  padding: 10px 0;
  border-bottom: 1px dashed #f0f0f0;
  align-items: center;
}

.schedule-item:last-child {
  border-bottom: none;
}

.time {
  width: 90px;
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.content {
  flex: 1;
  font-size: 14px;
  color: #555;
}

.platform {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 6px;
  border-radius: 4px;
  background-color: #e8f4ff;
  color: #0084ff;
  font-size: 12px;
}

.platform-wait {
  display: inline-block;
  margin-left: 8px;
  padding: 2px 6px;
  border-radius: 4px;
  background-color: #f29d72;
  color: #ffffff;
  font-size: 12px;
}

.postponement {
  display: inline-block;
  margin-right: 8px;
  padding: 2px 6px;
  border-radius: 4px;
  color: #ffffff;
  font-size: 12px;
  background-color: #ef5c25;
}

.platform-close {
  background-color: #ff4d4f;
}

.platform-end {
  background-color: #7bc47f;
}

.platform-ing {
  background-color: #ae5ef2;
}

.platform-close2 {
  background-color: #da393b;
}

.platform-postponement {
  background-color: #ef5c25;
}

/* 待处理数量徽章 */
.badge {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  border-radius: 50%;
  background-color: #ff4d4f;
  color: #fff;
  font-size: 12px;
  font-weight: 500;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .left-column {
    width: 100%;
  }
}

.action-btn {
  display: flex;
  align-items: center;
  padding: 5px 20px;
  border-radius: 8px;
  background-color: #f8f9fa;
  color: #333;
  border: none;
  transition: all 0.2s;
}

.action-btn:hover {
  background-color: #e9ecef;
  color: #0084ff;
}

.action-btn i {
  margin-right: 8px;
  font-size: 12px;
}

.action-btn span {
  font-size: 12px;
}

.over-btn {
  display: flex;
  align-items: center;
  padding: 5px 20px;
  border-radius: 8px;
  background-color: #f8f9fa;
  color: #da393b;
  border: none;
  transition: all 0.2s;
}

.over-btn:hover {
  background-color: #d66500;
  color: #fff;
}

.over-btn i {
  margin-right: 8px;
  font-size: 12px;
}

.over-btn span {
  font-size: 12px;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  text-align: center;
}

.empty-state i {
  font-size: 48px;
  color: #ddd;
  margin-bottom: 15px;
}

.empty-state p {
  margin: 0 0 20px 0;
  color: #999;
  font-size: 16px;
}
</style>
